package com.lagou.container;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

public class StandardHost{

    private String name;
    private String appBase;
    private Map<String, StandardContext> contextMap = new HashMap<String, StandardContext>();

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAppBase() {
        return appBase;
    }

    public void setAppBase(String appBase) {
        this.appBase = appBase;
    }

    public Map<String, StandardContext> getContextMap() {
        return contextMap;
    }

    public void setContextMap(Map<String, StandardContext> contextMap) {
        this.contextMap = contextMap;
    }

    public StandardHost(String name, String appBase) {
        this.name = name;
        this.appBase = appBase;
    }

    public StandardHost() {
    }

    public void init() {
        //获取webapp所在目录
        File webapp = new File(appBase);
        if(webapp.exists() && webapp.isDirectory()){
            //获取webapp下一级目录，即context
            File[] contextList = webapp.listFiles();
            for (int i = 0; i < contextList.length; i++) {
                String name = contextList[i].getName();
                String baseUrl = appBase + "/" + name;
                StandardContext standardContext = new StandardContext(name, baseUrl);
                //将"/"+应用包包名作为key，保存context对象
                contextMap.put("/" + name, standardContext);
                //context对象初始化
                standardContext.init();
            }
        }

    }
}
